package com.zhuiyun.project.api.plan.fitnessPlan.service.impl;

import com.zhuiyun.project.api.plan.fitnessPlan.entity.FitnessPlan;
import com.zhuiyun.project.api.plan.fitnessPlan.mapper.FitnessPlanMapper;
import com.zhuiyun.project.api.plan.fitnessPlan.model.FitnessPlanModel;
import com.zhuiyun.project.api.plan.fitnessPlan.service.FitnessPlanService;
import com.zhuiyun.project.api.plan.fitnessType.entity.FitnessType;
import com.zhuiyun.project.api.plan.fitnessType.mapper.FitnessTypeMapper;
import com.zhuiyun.project.api.plan.fitnessType.service.FitnessTypeService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName FitnessActionServiceImpl
 * @Description TODO
 * @Author ys
 * @Date 2023/11/2 9:01
 **/
@Service
public class FitnessPlanServiceImpl implements FitnessPlanService {
    @Resource
    private FitnessPlanMapper fitnessPlanMapper;
    /**
     * @Author 杨珅
     * @Description todo 健身计划分页查询
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public PageResult selectAll(PageRequest pageRequest, String fitnessPlanName,String venueUniqueIdentifier,Integer fitnessSeriesId) {
        MybatisPageHelper.startPage(pageRequest);// 使用MybatisPageHelper实现分页功能，设置分页信息
        List<FitnessPlanModel> PlanList = fitnessPlanMapper.selectAll(pageRequest, fitnessPlanName,venueUniqueIdentifier,fitnessSeriesId);// 调用ArticleReviewMapper的getArticle方法查询文章列表\
        return MybatisPageHelper.getPageResult(PlanList);
    }
    /**
     * @Author 杨珅
     * @Description todo 根据学院id查询计划
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult selectPlanById(Integer memberId) {
        List<FitnessPlanModel> fitnessPlanModels = fitnessPlanMapper.selectPlanById(memberId);
        if(fitnessPlanModels.isEmpty()){
            return CommonResult.ok(null);
        }else{
            return CommonResult.ok(fitnessPlanMapper.selectPlanById(memberId));
        }
    }

    /**
     * @Author 杨珅
     * @Description todo 健身计划新增
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult insertFitnessPlan(FitnessPlanModel fitnessPlan)throws CommonException {
        if (ValidatorUtil.ValidationObject(fitnessPlan)){
            fitnessPlan.setCreateTime(new Date());
            fitnessPlan.setUpdateTime(new Date());
            fitnessPlan.setEnableStatus("0");
            int i = fitnessPlanMapper.insertFitnessPlan(fitnessPlan);
            if (i >= 1) {

                return CommonResult.ok(fitnessPlan.getId());
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Author 杨珅
     * @Description todo 健身计划修改
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult updateFitnessPlan(FitnessPlan fitnessPlan)throws CommonException {
        if (ValidatorUtil.ValidationObject(fitnessPlan)){
            int i = fitnessPlanMapper.updateFitnessPlan(fitnessPlan);
            if (i >= 1) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        } else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
     * @Author 杨珅
     * @Description todo 健身计划逻辑删除
     * @Date 2023/11/13 9:01
     * @Return  * @param null
     */
    @Override
    public CommonResult deleteFitnessPlan(FitnessPlan fitnessPlan) {
        return CommonResult.ok(fitnessPlanMapper.deleteFitnessPlan(fitnessPlan));
    }
}
